package cn.bytecollege.admin.controller;

import cn.bytecollege.admin.service.ClassifyService;
import cn.bytecollege.admin.service.ProductService;
import cn.bytecollege.common.constant.Resource;
import cn.bytecollege.common.entity.Classify;
import cn.bytecollege.common.entity.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 商品修改前先回显数据，这个控制器就是用来回显数据的，也能用它来实现点击查看商品详情页。
 */
@WebServlet(name = "ProductFindController",value = "/admin/findProduct")
public class ProductFindController extends HttpServlet {
    private ProductService productService = null;
    private ClassifyService classifyService = null;
    public ProductFindController(){
        productService = new ProductService();
        classifyService = new ClassifyService();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Integer productId = Integer.parseInt(id);

        List<Classify> list = classifyService.findClassify();
        Product product = productService.findClassifyById(productId);

        request.setAttribute("product",product);
        request.setAttribute("list",list);
        request.getRequestDispatcher("/admin/html/device/productUpdate.jsp").forward(request,response);
    }
}
